package com.terrace.core.common.model;

import java.io.Serializable;
import java.text.MessageFormat;

import org.apache.commons.lang.StringUtils;

import com.terrace.core.constant.StatusConstant;

/**
 * 返回结果
 * 
 * @author jiangyg
 *
 */
public class ResultData implements Serializable {

	private static final long serialVersionUID = -5122784351926511952L;

	/**
	 * 状态码
	 */
	private String status = "10000";

	/**
	 * 返回消息
	 */
	private String message;

	/**
	 * 结果数据
	 */
	private Object result;

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public void setStatus(String status, Object... args) {
		this.status = status;
		this.initMessageByStatus(args);
	}

	public String getMessage() {
		if (StringUtils.isBlank(this.message)) {
			this.message = StatusConstant.message(this.status);
		}
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	/**
	 * 请求成功--默认状态吗：10000
	 * 
	 * @param message
	 *            成功消息
	 * @return
	 */
	public ResultData success(String message) {
		this.status = "10000";
		this.message = message;
		return this;
	}

	/**
	 * 请求成功
	 * 
	 * @param status
	 *            状态码
	 * @param message
	 *            成功消息
	 * @return
	 */
	public ResultData success(String status, String message) {
		this.status = status;
		this.message = message;
		return this;
	}

	/**
	 * 请求成功
	 * 
	 * @param status
	 *            状态码
	 * @param args
	 *            参数
	 * @return
	 */
	public ResultData successObjs(String status, Object... args) {
		this.status = status;
		this.initMessageByStatus(args);
		return this;
	}

	/**
	 * 请求失败--默认错误码：13000
	 * 
	 * @param message
	 *            错误消息
	 * @return
	 */
	public ResultData fail(String message) {
		this.status = "13000";
		this.message = message;
		return this;
	}

	/**
	 * 请求失败
	 * 
	 * @param status
	 *            错误码
	 * @param message
	 *            错误消息
	 * @return
	 */
	public ResultData fail(String status, String message) {
		this.status = status;
		this.message = message;
		return this;
	}

	/**
	 * 请求失败
	 * 
	 * @param status
	 *            错误码
	 * @param args
	 *            参数
	 * @return
	 */
	public ResultData failObjs(String status, Object... args) {
		this.status = status;
		this.initMessageByStatus(args);
		return this;
	}

	/**
	 * 初始化message
	 * 
	 * @param args
	 */
	private void initMessageByStatus(Object... args) {
		String formatStr = StatusConstant.message(this.status);
		if (StringUtils.isNotBlank(formatStr)) {
			this.message = MessageFormat.format(formatStr, args);
		}
	}

}
